package cn.jbolt.admin.msgcenter;

import cn.jbolt.core.api.JBoltApiBaseController;
import cn.jbolt.core.api.httpmethod.JBoltHttpGet;
import cn.jbolt.core.api.httpmethod.JBoltHttpPost;
import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.kit.JBoltUserKit;
import cn.jbolt.core.model.Todo;
import cn.jbolt.core.model.base.JBoltBaseModel;
import cn.jbolt.core.permission.UnCheck;
import cn.jbolt.core.service.JBoltTodoService;
import cn.jbolt.core.service.base.JBoltBaseService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.core.paragetter.Para;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.tx.Tx;
@UnCheck
@Path("/api/admin/todo")
public class TodoAdminController extends JBoltApiBaseController {

    @Inject
    private JBoltTodoService service;

    @Override
    protected JBoltBaseService<? extends JBoltBaseModel<?>> getModelService() {
        return service;
    }

    /**
     * 单个详情
     */
    @JBoltHttpGet
    public void index() {
        Ret ret = findModelById();
        if(ret.isFail()){
            renderJBoltApiRet(ret);
            return;
        }
        Todo todo = ret.getAs("data");
        Long userId = JBoltUserKit.getUserId();
        if(!todo.getUserId().equals(userId)) {
            renderJBoltApiFail(JBoltMsg.NOPERMISSION);
            return;
        }
        renderJBoltApiRet(ret);
    }

    /**
     * 保存
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void save(@Para("")Todo todo) {
        renderJBoltApiRet(service.save(todo));
    }

    /**
     * 更新
     */
    @Before(Tx.class)
    @JBoltHttpPost
    public void update(@Para("")Todo todo) {
        renderJBoltApiRet(service.update(todo));
    }

    /**
     * 删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void delete() {
        renderJBoltApiRet(service.deleteById(getId()));
    }
    /**
     * 批量删除
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void deleteByIds() {
        renderJBoltApiRet(service.deleteMyTodoByBatchIds(getIds()));
    }
    /**
     * 改状态
     */
    @Before(Tx.class)
    public void updateState(@Para("id")Long id,@Para("state")Integer state) {
        renderJBoltApiRet(service.updateState(id,state));
    }
    /**
     * 批量改状态
     */
    @Before(Tx.class)
    @JBoltHttpGet
    public void batchUpdateState(@Para("ids")String ids,@Para("state")Integer state) {
        renderJBoltApiRet(service.batchUpdateState(ids,state));
    }

}
